package com.jp.knowledge.my.adapter;

import android.content.Context;
import android.content.SharedPreferences;
import android.support.annotation.NonNull;
import android.util.TypedValue;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.jp.knowledge.R;
import com.jp.knowledge.adapter.JPRecycleViewHolder;
import com.jp.knowledge.adapter.KnowlageItemAdapter;
import com.jp.knowledge.my.base.RecycleVAdapter;
import com.jp.knowledge.my.base.RecycleVHolder;
import com.jp.knowledge.my.model.CollectionAlbumModel;
import com.jp.knowledge.util.SharedUtil;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Locale;

import static android.content.Context.MODE_PRIVATE;

/**
 * Created by 528787067@qq.com on 2017/7/14.
 */

public class CollectionAlbumAdapter  extends RecycleVAdapter<CollectionAlbumModel> implements View.OnClickListener {

	private int textSize;
	private SharedPreferences sharedPreferences;
	private DateFormat dateFormat;

	public CollectionAlbumAdapter(Context context, @NonNull List<CollectionAlbumModel> list) {
		super(context, list);
		dateFormat = new SimpleDateFormat("MM月dd日 hh:mm", Locale.getDefault());
		sharedPreferences = mContext.getSharedPreferences("NewsDetail", MODE_PRIVATE);
	}

	@Override
	public int getLayoutId(int viewType) {
		return R.layout.item_collection_album;
	}

	@Override
	public void onBind(RecycleVHolder holder, int position) {
		CollectionAlbumModel data = getData(position);
		TextView packageTitle = holder.getTextView(R.id.packageTitle);
		packageTitle.setText(data.getPackageTitle());
		if(textSize!=0)
			packageTitle.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);
		else
			packageTitle.setTextSize(TypedValue.COMPLEX_UNIT_PX, mContext.getResources().getDimension(R.dimen.middle_font));

		holder.setTextView(R.id.createTime, dateFormat.format(data.getCreatetime()));
		holder.setImageUrl(R.id.packageCover, data.getPackageCover());
		View closeIcon = holder.get(R.id.iconClose);
		closeIcon.setTag(position);
		closeIcon.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		if(itemCloseClickListner != null)
			itemCloseClickListner.onItemCloseClick((int)v.getTag());
	}

	public void changeTextSize(){
		getTextSize();
		notifyDataSetChanged();
	}

	private void getTextSize(){
		int size = sharedPreferences.getInt("textSizeLevel", 1);
		this.textSize = SharedUtil.getInstance(mContext).getCommFontSize();
		switch (size){
			case 0:
				this.textSize = (int)(mContext.getResources().getDimension(R.dimen.min_font)+0.5);
				break;
			case 1:
				this.textSize = (int)(mContext.getResources().getDimension(R.dimen.middle_font)+0.5);
				break;
			case 2:
				this.textSize = (int)(mContext.getResources().getDimension(R.dimen.big_font)+0.5);
				break;
			case 3:
				this.textSize = (int)(mContext.getResources().getDimension(R.dimen.large_font)+0.5);
				break;
			default:
				this.textSize = (int)(mContext.getResources().getDimension(R.dimen.middle_font)+0.5);
		}
	}

	private ItemCloseClickListner itemCloseClickListner;

	public void setItemCloseClickListner(ItemCloseClickListner itemCloseClickListner) {
		this.itemCloseClickListner = itemCloseClickListner;
	}

	public interface ItemCloseClickListner{
		void onItemCloseClick(int position);
	}
}
